package bll

import (
	"context"
	"github.com/LyricTian/gin-admin/v7/internal/app/model/gormx/model"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/LyricTian/gin-admin/v7/pkg/errors"
	"github.com/LyricTian/gin-admin/v7/pkg/util/uuid"
	"github.com/google/wire"
)

// UserInfoSet 注入UserInfo
var UserInfoSet = wire.NewSet(wire.Struct(new(UserInfo), "*"))

// UserInfo 用户信息
type UserInfo struct {
	UserInfoModel *model.UserInfo
}

// Query 查询数据
func (a *UserInfo) Query(ctx context.Context, params schema.UserInfoQueryParam, opts ...schema.UserInfoQueryOptions) (*schema.UserInfoQueryResult, error) {
	return a.UserInfoModel.Query(ctx, params, opts...)
}

// GetFirst 查询数据
func (a *UserInfo) GetFirst(ctx context.Context, params schema.UserInfoQueryParam, opts ...schema.UserInfoQueryOptions) (*schema.UserInfo, error) {
	item, err := a.UserInfoModel.GetFirst(ctx, params, opts...)
	if err != nil {
		return nil, err
	}
	if item == nil {
		return nil, errors.ErrNotFound
	}

	return item, nil
}

// Get 查询指定数据
func (a *UserInfo) Get(ctx context.Context, id string, opts ...schema.UserInfoQueryOptions) (*schema.UserInfo, error) {
	item, err := a.UserInfoModel.Get(ctx, id, opts...)
	if err != nil {
		return nil, err
	} else if item == nil {
		return nil, errors.ErrNotFound
	}

	return item, nil
}

// Create 创建数据
func (a *UserInfo) Create(ctx context.Context, item schema.UserInfo) (*schema.IDResult, error) {

	item.ID = uuid.NewID()
	err := a.UserInfoModel.Create(ctx, item)
	if err != nil {
		return nil, err
	}

	return schema.NewIDResult(item.ID), nil
}

// Update 更新数据
func (a *UserInfo) Update(ctx context.Context, id string, item schema.UserInfo) error {
	_, err := a.Get(ctx, id)
	if err != nil {
		return err
	}

	return a.UserInfoModel.Update(ctx, id, item)
}

// Delete 删除数据
func (a *UserInfo) Delete(ctx context.Context, id string) error {
	item, err := a.UserInfoModel.Get(ctx, id)
	if err != nil {
		return err
	}
	if item == nil {
		return errors.ErrNotFound
	}

	return a.UserInfoModel.Delete(ctx, id)
}

// UpdateStatus 更新状态
func (a *UserInfo) UpdateStatus(ctx context.Context, id string, status int) error {
	item, err := a.UserInfoModel.Get(ctx, id)
	if err != nil {
		return err
	}
	if item == nil {
		return errors.ErrNotFound
	}

	return a.UserInfoModel.UpdateStatus(ctx, id, status)
}
